<?php
namespace  app\man\controller;

use think\Controller;
use think\Db;
use think\Paginator;
use think\Request;
use think\Cache;
class  Ad extends Common
{
	public function  pic()
	{
		$pics = Db::name('pic_list')->select();

		$this->assign('pics',$pics);

		return $this->fetch('pic');
	}

	public function  txt()
	{
		$txt = Db::name('txt_ad')->paginate();

		$this->assign('txt',$txt);

		return $this->fetch('txt');
	}

	public function delete()
	{
		$id = input('aid');
		$type=input('type');
		if($type==1){
			$table='pic_list';
		}else{
			$table='txt_ad';
		}
		$res = Db::name($table)->where('id',$id)->delete();

		if($res){
			return true;
		}else{
			return false;
		}
	}


	public function  add_txt(){

		 if(Request::instance()->isPost()){
			$data=Request::instance()->post();
			$data['ptime']=time();
			$res=Db::name('txt_ad')->insert($data);
			if($res){
				cache::rm('txt_ad');
				$this->success('添加成功',url('man/ad/txt'));
			}else{
				$this->success('添加失败',url('man/ad/txt'));
			}
		}

		return $this->fetch('add_txt');
	}


	public function edit_txt(){
		$id=input('id');
		 if(Request::instance()->isPost()){
			$data=Request::instance()->post();
			$data['ptime']=time();
			unset($data['id']);
			$res=Db::name('txt_ad')->where('id',$id)->update($data);
			if($res){
				cache::rm('txt_ad');

				$this->success('修改成功',url('man/ad/txt'));
			}else{
				$this->success('修改失败',url('man/ad/txt'));
			}
		}

		$con = Db::name('txt_ad')->where('id',$id)->find();
		$this->assign('con',$con);
		return $this->fetch('edit_txt');
	}

	public function edit_pic(){
		$id=input('id');
		if(Request::instance()->isPost()){
			//vendor('ucloud.proxy');
			if(!$config=cache::store("redis")->get('config')){
				$config = Db::name('sys_config')->where('id',1)->find();
				cache::store("redis")->set('config',$config,600);
			}

			$data=Request::instance()->post();

	        $res=Db::name('pic_list')->where('id',$id)->update($data);
	        if($res){
	        	cache::rm('detail_top_ad');
	        	cache::rm('detail_bottom_ad');
	        	cache::rm('answer_top_ad');
	        	cache::rm('answer_bottom_ad');
				$this->success('修改成功',url('man/ad/txt'));
			}else{
				$this->success('修改失败',url('man/ad/txt'));
			}
		}
		$con = Db::name('pic_list')->where('id',$id)->find();
		$this->assign('con',$con);
		return $this->fetch('edit_pic');
	}

	public function add_pic(){

		if(Request::instance()->isPost()){
			//vendor('ucloud.proxy');
			if(!$config=cache::store("redis")->get('config')){
				$config = Db::name('sys_config')->where('id',1)->find();
				cache::store("redis")->set('config',$config,600);
			}
			$data=Request::instance()->post();

	        $data['ptime']=time();
	        $result = Db::name('pic_list')->insert($data);
	        if ($result) {
	        	cache::rm('detail_top_ad');
	        	cache::rm('detail_bottom_ad');
	        	cache::rm('answer_top_ad');
	        	cache::rm('answer_bottom_ad');
	            $this->success('添加成功！', url('man/ad/pic'));
	        } else {
	            $this->error('添加失败！');
	        }
		}
		return $this->fetch('add_pic');
	}

}


?>
